#coding=utf-8

create_gbjg_sql = '''
create table if not exists gbjg
    (id int not null primary key auto_increment,
    code char(6) not null,
    changeday date not null,
    publicday date ,
    reason tinytext,
    zgb bigint ,
    ltag bigint not null,
    xsag bigint)
'''

def get_ltag(con, code, day):
    sql = 'select changeday, ltag from gbjg where code=%s order by changeday'

    c = con.cursor()
    c.execute(sql, code)
    r = c.fetchall()
    c.close()

    if not r: return r
    data = [(i[0].isoformat(), i[1])for i in r]
    
    for i in range(len(data)):
        d = data[i]
        if d[0] > day: 
            index = i - 1
            if index <  0: index = 0
            return data[index][1]
    
    return data[-1][1]
        
    
        
